package com.ktjiaoyu;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTester {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Test
    public void test(){
        //操作字符串类型数据
        ValueOperations<String,Object> vo = redisTemplate.opsForValue();
        ValueOperations<String, String> vo2 = stringRedisTemplate.opsForValue();

//        //新增
//        vo.set("name","zhangsan");
//        //查询
//        Object name = vo.get("name");
//        System.out.println(name);
        //新增并设置数据的有效期
//        vo.set("age",18,10, TimeUnit.SECONDS);

        //修改
//        vo.set("name","lisi");

        //删除
//        redisTemplate.delete("name");

        redisTemplate.delete("count");

        vo.set("count",1);
        //加1
        vo.increment("count",1);
        //减1
        vo.decrement("count",1);

    }
    
    @Test
    public void test2(){
        HashOperations<String,String,Object> ho = redisTemplate.opsForHash();

        //新增
//        ho.put("myhash","name","zhangsan");
//        ho.put("myhash","age",18);
        //查询
//        Object o = ho.get("myhash", "name");
//        System.out.println(o);
        //修改
//        ho.put("myhash","name","lisi");
//        Object o = ho.get("myhash", "name");
//        System.out.println(o);
        //删除
//        ho.delete("myhash","name");
//        Object o = ho.get("myhash", "name");
//        System.out.println(o);

        //查询所有
        Map<String, Object> map = ho.entries("myhash");
        for (String s : map.keySet()) {
            System.out.println(s+":"+map.get(s));
        }
    }
    @Test
    public void test3(){
        ListOperations<String,String> lo = redisTemplate.opsForList();

//        //新增
//        lo.leftPush("mylist","a");
//        lo.rightPush("mylist","b");
//        lo.leftPush("mylist","c");
//
//        //查询所有
//        List<String> list = lo.range("mylist", 0, -1);
//        for (String s : list) {
//            System.out.println(s);
//        }

        //删除
        String one = lo.leftPop("mylist");
        String two =lo.rightPop("mylist");
        //查询所有
        List<String> list = lo.range("mylist", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
    }

    @Test
    public void test4(){
        SetOperations<String,String> so = redisTemplate.opsForSet();

//        //新增
//        so.add("myset","a");
//        so.add("myset","a");
//        so.add("myset","b");
//        so.add("myset","c");
//
//        //查询所有
//        Set<String> myset = so.members("myset");
//        for (String s : myset) {
//            System.out.println(s);
//        }

        //删除
        so.remove("myset","c");
        Set<String> myset = so.members("myset");
        for (String s : myset) {
            System.out.println(s);
        }

    }

    @Test
    public void test5(){
        ZSetOperations zso = redisTemplate.opsForZSet();

        //新增
//        zso.add("myzset","a",9);
//        zso.add("myzset","a",2);
//        zso.add("myzset","b",11);
//        zso.add("myzset","c",3);
//
//        //查询所有
//        Set<String> myset = zso.range("myzset",0,-1);
//        for (String s : myset) {
//            System.out.println(s);
//        }

        //删除
        zso.remove("myzset","b");
        Set<String> myset = zso.range("myzset",0,-1);
        for (String s : myset) {
            System.out.println(s);
        }
    }

}
